package com.example.mydemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.DailyExpenseEsRepository;

import com.example.entity.DailyExpense;
import com.example.mydemo.entity.LogBean;
import com.example.mydemo.mapper.DailyExpenseMapper;
import com.example.mydemo.service.DailyExpenseService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class DailyExpenseServiceImpl extends ServiceImpl<DailyExpenseMapper, DailyExpense> implements DailyExpenseService {


    private DailyExpenseEsRepository esRepository; // ES仓库

    // 费用类型字典（含多种日常场景）
    private static final String[] EXPENSE_TYPES = {
            "办公用品", "差旅费", "水电费", "物业费", "交通费",
            "业务招待费", "设备维修费", "网络通讯费", "团建活动费", "培训费"
    };

    // 部门字典
    private static final String[] DEPARTMENTS = {
            "研发部", "市场部", "销售部", "财务部", "人力资源部",
            "行政部", "客服部", "产品部"
    };

    // 申请人姓名（随机中文姓名）
    private static final String[] APPLICANTS = {
            "张三", "李四", "王五", "赵六", "孙七", "周八", "吴九", "郑十",
            "钱十一", "孙十二", "李十三", "周十四", "吴十五"
    };

    // 费用描述模板（含大量汉字，模拟真实场景）
    private static final String[] DESCRIPTION_TEMPLATES = {
            "%s采购：A4纸5箱、签字笔20支、文件夹10个",
            "%s：前往北京出差3天，交通+住宿",
            "%s：2024年6月办公室电费",
            "%s：2024年第二季度物业费",
            "%s：员工外出办事打车费（3次）",
            "%s：接待客户晚餐（5人）",
            "%s：打印机维修（更换硒鼓）",
            "%s：公司宽带+固定电话费",
            "%s：部门聚餐（15人）",
            "%s：产品经理技能培训课程"
    };

    // 审批备注模板
    private static final String[] REMARK_TEMPLATES = {
            "费用合理，同意报销",
            "附件齐全，已审批",
            "金额超预算，需补充说明",
            "不符合报销标准，驳回",
            "已核实，同意支付",
            "部门经理已审批，同意流程"
    };

    @Override
    public boolean batchGenerateAndSave(int count) {
        List<DailyExpense> expenses = new ArrayList<>(count);
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            DailyExpense expense = new DailyExpense();

            // 1. 生成费用单号（规则：EXP+日期+3位序号）
            String dateStr = LocalDate.now().toString().replace("-", "");
            String seq = String.format("%03d", i % 1000);
            expense.setExpenseNo("EXP" + dateStr + seq);

            // 2. 随机选择费用类型
            int typeIdx = random.nextInt(EXPENSE_TYPES.length);
            String type = EXPENSE_TYPES[typeIdx];
            expense.setType(type);

            // 3. 生成费用描述（替换模板中的%s为类型）
            String descTemplate = DESCRIPTION_TEMPLATES[typeIdx];
            expense.setDescription(String.format(descTemplate, type));

            // 4. 生成金额（按类型区分范围）
            BigDecimal amount = generateAmountByType(type, random);
            expense.setAmount(amount);

            // 5. 随机生成发生日期（近3个月内）
            LocalDate today = LocalDate.now();
            LocalDate expenseDate = today.minus(random.nextInt(90), ChronoUnit.DAYS);
            expense.setExpenseDate(expenseDate);

            // 6. 随机选择部门、申请人、状态
            expense.setDepartment(DEPARTMENTS[random.nextInt(DEPARTMENTS.length)]);
            expense.setApplicant(APPLICANTS[random.nextInt(APPLICANTS.length)]);
            expense.setStatus(randomStatus(random));

            // 7. 随机生成备注
            expense.setRemark(REMARK_TEMPLATES[random.nextInt(REMARK_TEMPLATES.length)]);

            expenses.add(expense);
        }

        // 批量保存到数据库
//        boolean dbSuccess = saveBatch(expenses, 500);
        // 批量同步到ES
//        if (dbSuccess) {
            esRepository.saveAll(expenses);
            log.info("成功生成并保存 {} 条日常用度数据", count);
            return true;
//        }
//        return false;
    }

    // 按费用类型生成合理金额范围
    private BigDecimal generateAmountByType(String type, Random random) {
//        return switch (type) {
//            case "办公用品" -> new BigDecimal(random.nextInt(500, 2000));
//            case "差旅费" -> new BigDecimal(random.nextInt(1000, 5000));
//            case "水电费" -> new BigDecimal(random.nextInt(2000, 8000));
//            case "物业费" -> new BigDecimal(random.nextInt(5000, 20000));
//            case "交通费" -> new BigDecimal(random.nextInt(100, 500));
//            case "业务招待费" -> new BigDecimal(random.nextInt(800, 3000));
//            case "设备维修费" -> new BigDecimal(random.nextInt(300, 1500));
//            case "网络通讯费" -> new BigDecimal(random.nextInt(500, 1500));
//            case "团建活动费" -> new BigDecimal(random.nextInt(2000, 10000));
//            case "培训费" -> new BigDecimal(random.nextInt(3000, 15000));
//            default -> new BigDecimal(random.nextInt(100, 1000));
//        };
        return  null;
    }

    // 生成状态（按概率分布：已审批>待审批>已报销>驳回）
    private String randomStatus(Random random) {
        int rand = random.nextInt(100);
        if (rand < 50) return "已审批";
        else if (rand < 70) return "待审批";
        else if (rand < 90) return "已报销";
        else return "驳回";
    }
// Elasticsearch 基本分页查询
}
